package com.ethereal.petmanagement.service.impl;

import com.ethereal.petmanagement.dao.IPetDao;
import com.ethereal.petmanagement.dao.impl.PetDaoImpl;
import com.ethereal.petmanagement.dto.PageDTO;
import com.ethereal.petmanagement.dto.PetDTO;
import com.ethereal.petmanagement.dto.ResponseDTO;
import com.ethereal.petmanagement.service.IPetService;
import com.ethereal.petmanagement.util.CodeMsg;
import com.ethereal.petmanagement.util.PasswordUtil;

import java.sql.Timestamp;
import java.util.List;

public class PetServiceImpl implements IPetService {
    static IPetDao petDao = new PetDaoImpl();

    //添加宠物
    @Override
    public ResponseDTO<Boolean> addPet(PetDTO petDTO) {
        petDTO.setId(PasswordUtil.generateId());
        petDTO.setCreateTime(new Timestamp(System.currentTimeMillis()));
        petDTO.setPetType(1);
        ResponseDTO<Integer> responseDTO = petDao.addPet(petDTO);
        if (responseDTO.getData() == 0){
            return ResponseDTO.errorByMsg(CodeMsg.PET_ADD_ERROR);
        }
        return ResponseDTO.success(true);
    }

    //获取宠物列表
    @Override
    public ResponseDTO<PageDTO<PetDTO>> getPetList(PetDTO petDTO) {
        List<PetDTO> petDTOList = petDao.getPetList(petDTO);
        PageDTO<PetDTO> pageDTO = new PageDTO<>();
        pageDTO.setList(petDTOList);
        return ResponseDTO.success(pageDTO);
    }

    //删除宠物
    public ResponseDTO<Boolean> deletePet(PetDTO petDTO){
        if (petDTO.getId() == null){
            return ResponseDTO.errorByMsg(CodeMsg.PET_DELETE_ERROR);
        }
        ResponseDTO<Integer> responseDTO = petDao.deletePet(petDTO);
        if (responseDTO.getData() == 0){
            return ResponseDTO.errorByMsg(CodeMsg.PET_DELETE_ERROR);
        }
        return ResponseDTO.success(true);
    }

    //修改宠物
    @Override
    public ResponseDTO<Boolean> updatePet(PetDTO petDTO) {
        System.out.println("sss");
        ResponseDTO<Integer> responseDTO = petDao.updatePet(petDTO);
        if (responseDTO.getData() == 0){
            return ResponseDTO.errorByMsg(CodeMsg.PET_ADD_ERROR);
        }
        return ResponseDTO.success(true);
    }
}
